<?php

namespace Wilead\ApiBundle\Controller;

use FOS\RestBundle\Controller\Annotations\RequestParam,
    FOS\RestBundle\View\RouteRedirectView;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BlacklistController
 *
 * @author jack
 */
class BlacklistController extends ApiController {

    /**
     * @ApiDoc(
     *      description="1202获取用户黑名单接口"
     * )
     */
    public function getAction($id) {
        $qb = $this->getRepository('WileadIArtistBundle:Blacklist')->createQueryBuilder('q');
        $result = $qb
                ->select('q, t, r')
                ->leftJoin('q.Tid', 't')
                ->leftJoin('t.UserResume', 'r')
                ->where('q.Uid = :id')->setParameter('id', $id)
                ->orderBy('q.id', 'DESC')
                ->getQuery()
                ->getArrayResult()
        ;

        return $this->getFOSView()->setData($result)->setStatusCode(200);
    }

    /**
     * @ApiDoc(
     *      description="1203移除黑名单记录接口"
     * )
     * @RequestParam(name="id", requirements="integer", description="要移除的记录ID")
     */
    public function deleteAction() {
        $em = $this->getEntityManager();
        $request = $this->getRequest()->request;
        $blackList = $this->getRepository('WileadIArtistBundle:Blacklist')->find($request->get('id'));

        if ($blackList) {
            $uid = $blackList->getUid()->getId();
            $em->remove($blackList);
            $em->flush();
            return RouteRedirectView::create('wilead_api_blacklist_get', array('id' => $uid), 200);
        }

        return $this->getFOSView()->setStatusCode(404);
    }

    /**
     * @ApiDoc(
     *      description="1201添加到黑名单接口"
     * )
     * @RequestParam(name="uid", requirements="integer", description="添加人ID")
     * @RequestParam(name="tid", requirements="integer", description="被添加人ID")
     */
    public function addAction() {
        $em = $this->getEntityManager();
        $request = $this->getRequest()->request;
        $blacklist = new \Wilead\IArtistBundle\Entity\Blacklist;

        $repository = $this->getRepository('WileadIArtistBundle:User');

        /* @var $user \Wilead\IArtistBundle\Entity\User */
        $user = $repository->find($request->get('uid'));
        /* @var $targetUser \Wilead\IArtistBundle\Entity\User */
        $targetUser = $repository->find($request->get('tid'));

        $blacklist->setUid($user);
        $blacklist->setTid($targetUser);

        // 删除好友关系
        $user->removeFriend($targetUser);
        $targetUser->removeFriend($user);

        $em->persist($user);
        $em->persist($targetUser);
        $em->persist($blacklist);
        $em->flush();

        return $this->getFOSView()->setData('添加成功')->setStatusCode(200);
    }

}

?>
